package multithread.lianbiao;

/**
 * Created by xi on 2019/2/24.
 */
public class NodeUtil {

    public Node reverse(Node node) {
        if (node == null || node.next == null){
            return node;
        }
        Node newHead = reverse(node.next);
        node.next.next = node;//node节点的下个node.next下个节点.next 指向自己
        node.next = null; //node的next 指向为空
        return newHead;
    }

    /**
     * 递归反转
     * @param head
     * @return
     */
    public static Node reverseNew(Node head){
        //如果是空链表或者尾结点,就返回了，不做了
        if (head==null||head.getNext()==null) {
            return head;
        }
        //先反转后续结点，倒数第2个节点了
        Node reversedHead = reverseNew(head.getNext());
        //当前结点下个节点next指针指向前一结点
       // head.getNext().setNext(head);

        //当前节点的下一个节点的下个节点指向自己
        head.next.next = head;
        //令前一结点的指针域为null
        head.setNext(null);
        return reversedHead;
    }



}
